home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-src / vcpp / getopt.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  1KB  |  54 lines

  1. #include    <stdio.h>
  2. #define EPR                 fprintf(stderr,
  3. #define ERR(str, chr)       if(opterr){EPR "%s%c\n", str, chr);}
  4. int     opterr = 1;
  5. int     optind = 1;
  6. int    optopt;
  7. char    *optarg;
  8. char    *strchr();
  9.  
  10. int
  11. getopt (argc, argv, opts)
  12. char **argv, *opts;
  13. {
  14.     static int sp = 1;
  15.     register c;
  16.     register char *cp;
  17.  
  18.     if (sp == 1)
  19.         if (optind >= argc ||
  20.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  21.             return -1;
  22.         else if (strcmp(argv[optind], "--") == 0) {
  23.             optind++;
  24.             return -1;
  25.         }
  26.     optopt = c = argv[optind][sp];
  27.     if (c == ':' || (cp=strchr(opts, c)) == 0) {
  28.         ERR (": illegal option -- ", c);
  29.         if (argv[optind][++sp] == '\0') {
  30.             optind++;
  31.             sp = 1;
  32.         }
  33.         return '?';
  34.     }
  35.     if (*++cp == ':') {
  36.         if (argv[optind][sp+1] != '\0')
  37.             optarg = &argv[optind++][sp+1];
  38.         else if (++optind >= argc) {
  39.             ERR (": option requires an argument -- ", c);
  40.             sp = 1;
  41.             return '?';
  42.         } else
  43.             optarg = argv[optind++];
  44.         sp = 1;
  45.     } else {
  46.         if (argv[optind][++sp] == '\0') {
  47.             sp = 1;
  48.             optind++;
  49.         }
  50.         optarg = 0;
  51.     }
  52.     return c;
  53. }
  54.